package com.google.code.ptrends.catparsers;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.google.code.ptrends.catparsers.utils.IntPair;
import com.google.code.ptrends.common.entities.Category;
import com.google.code.ptrends.common.entities.Session;
import com.google.code.ptrends.entities.ETLItem;
import com.google.code.ptrends.excel.ExcelReader;
import com.google.code.ptrends.excel.entities.Worksheet;

public class CompositeCategoryParser implements ETLItemFactory {
	private Map<Category, ETLItemFactory> parsers;

	public CompositeCategoryParser(ExcelReader reader, Worksheet worksheet,
			Session session) {
		if (reader == null)
			throw new IllegalArgumentException("Illegal null-pointer reader");

		if (worksheet == null)
			throw new IllegalArgumentException("Illegal null-pointer worksheet");

		if (session == null)
			throw new IllegalArgumentException("Illegal null-reference session");

		parsers = new HashMap<Category, ETLItemFactory>();

		parsers.put(Category.CASE, new CaseCategoryParser(reader, worksheet,
				session));
		parsers.put(Category.CONTROLLER, new ControllerCategoryParser(reader,
				worksheet, session));
		parsers.put(Category.CPU, new CPUCategoryParser(reader, worksheet,
				session));
		parsers.put(Category.DRIVE, new DriveCategoryParser(reader, worksheet,
				session));
		parsers.put(Category.HDD, new HDDCategoryParser(reader, worksheet,
				session));
		parsers.put(Category.MONITOR, new MonitorCategoryParser(reader,
				worksheet, session));
		parsers.put(Category.MOTHERBOARD, new MotherboardCategoryParser(reader,
				worksheet, session));
		parsers.put(Category.RAM, new RAMCategoryParser(reader, worksheet,
				session));
		parsers.put(Category.SOUNDCARD, new SoundCardCategoryParser(reader,
				worksheet, session));
		parsers.put(Category.VIDEOCARD, new VideoCardCategoryParser(reader,
				worksheet, session));
	}

	@Override
	public ETLItem[] parse(Category category, int supplierID,
			IntPair categoryPositions) {
		List<ETLItem> result = new ArrayList<ETLItem>();
		ETLItemFactory parser = null;

		if (parsers.containsKey(category)) {
			parser = parsers.get(category);
			ETLItem[] items = parser.parse(category, supplierID,
					categoryPositions);
			result.addAll(Arrays.asList(items));
		} else {
			return new ETLItem[] {};
		}

		return (ETLItem[]) result.toArray(new ETLItem[result.size()]);
	}
}